package com.boot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.boot.entity.Role;
import com.boot.pojo.Result;
import com.boot.service.DictService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boot.service.IMenuService;
import com.boot.entity.Menu;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shuchong
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private IMenuService menuService;

    @Autowired
    private DictService dictService;

    //新增或者更新
    @PostMapping("/saveOrUpdate")
    public Result save(@RequestBody Menu menu){
        boolean flag;
        if(Objects.isNull(menu.getId())){
            flag = menuService.saveOrUpdate(menu);
            if(flag){
                return new Result().success("保存成功");
            }else{
                return new Result().fail("保存失败");
            }
        }else{
            flag = menuService.saveOrUpdate(menu);
            if(flag){
                return new Result().success("修改成功");
            }else{
                return new Result().fail("修改失败");
            }
        }
    }
    //删除单个
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable Integer id){
        boolean flag = menuService.removeById(id);
        if(flag){
            return new Result().success("删除成功");
        }else{
            return new Result().fail("删除失败");
        }
    }
    @GetMapping("/ids")
    public Result findAllIds(){
        return new Result().success().add("list",menuService.list().stream().map(menu -> menu.getId()));
    }
    //批量删除
    @PostMapping("/dels")
    public Result delete(@RequestBody List<Integer> ids){
        boolean flag = menuService.removeByIds(ids);
        if(flag){
            return new Result().success("执行成功");
        }else{
            return new Result().fail("执行失败");
        }
    }
    //查找全部
    @GetMapping("/findAll")
    public Result findAll(@RequestParam(defaultValue = "") String name){
        List<Menu> parentNodes = menuService.findMenus(name);
        return new Result().success().add("parentNode",parentNodes);
    }

    //获取数据
    @GetMapping("/getData/{name}")
    public Result getData(@PathVariable("name")String name){
        QueryWrapper<Menu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        Menu menu = menuService.getOne(queryWrapper);
        return new Result().success("数据获取成功").add("menu",menu);
    }

    //分页
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                                @RequestParam Integer pageNum,
                               @RequestParam Integer pageSize){
        IPage<Menu> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        if(!"".equals(name)){
            queryWrapper.like("name",name);
        }
        IPage<Menu> page1 = menuService.page(page,queryWrapper);
        return new Result().add("page",page1);
    }
    @GetMapping("/icons")
    public Result getIcons(){
            return new Result().success().add("icons",dictService.list());
    }

}
